package com.client.task;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;

import com.client.bean.BirdList;
import com.client.bean.BirdMessage;
import com.client.bean.User;
import com.client.common.ValueStack;
import com.client.db.DataService;
import com.client.net.Request;
import com.clinet.cache.Log;

public class UnUploadBirdMessagesTask extends
		AsyncTask<BirdList, Void, Integer> {
	public interface Callback {

		void onComplete(Integer result);
	}

	private final Context context;
	private Callback callback;
	// 查询列表：
	// private String path = ValueStack.ROOT
	// + "/forestry/birdMessage_getAll.action";
	private DataService mDataService = null;

	// private Request mRequest = null;

	public UnUploadBirdMessagesTask(Context context, Callback callback) {
		this.context = context;
		this.callback = callback;
		// mRequest = new Request(path);
		if (mDataService == null) {
			mDataService = new DataService(context);
		}
	}

	// 正确返回：
	// {"items":[{"birdId":"40289381368be15b01368c0804e20002",
	// "birdImage":"753726d6cdc444cd96ef4d0fd1c18d73.jpg","birdName":"fdasg",
	// "collectLoaction":"","date":"","deadDes":"fdsgfdsg",
	// "deadImage":"381ba853e1dc4114a3b9e19cf3ac7891.jpg","deadNum":0,"direct":"","distance":"","healthNum":0,
	// "kind":"23","latitude":"","longitude":"","num":232,"place":"","sickDes":"43",
	// "sickImage":"81c7484b13664748b5fad4fa60c6b23e.jpg","sickNum":0,"status":"","stayPlace":""}],
	// "message":"OK","success":true,"totalNum":1}
	// 错误返回：
	// {"success":false,"message":"请输入用户名或密码!"}//没用登录的情况

	/**
	 * The task should be executed with one given source photo uri.
	 */
	@Override
	protected Integer doInBackground(BirdList... params) {
		int code = 0;
		if (params[0] == null) {
			code = 0;
		} else {
			if (!iscancle) {
				// for (int i = 0; i < 30; i++) {
				// params[0].addBirdMessage(new BirdMessage());
				// }
				// ValueStack.getInstance().setValue("USER", params[0]);
//				if (!mDataService.hasBirdList()) {// 本地无数据进行更新
//					Log.i("本地无数据进行更新");
					// User muser = (User)
					// ValueStack.getInstance().getSessionValue(
					// "USER");
					// Map<String, String> ps = new HashMap<String, String>();
					// ps.put("username", muser.userName);
					// ps.put("password", muser.password);
					// path = path + ";jsessionid" + muser.sessionid;
					// // Collections.sort(params[0].getItems(), new
					// // Price91ComparatorDown());
					// try {
					// String temp = mRequest.postFormData(ps);
					// Log.i("postText" + temp);
					// JSONObject obj = new JSONObject(temp);
					// boolean falg = obj.getBoolean("success");
					// if (falg) {
					// code = 1;
					// JSONArray items = obj.getJSONArray("items");
					// BirdMessage mBirdMessage = null;
					// for (int i = 0; i < items.length(); i++) {
					// JSONObject o = items.getJSONObject(i);
					// mBirdMessage = new BirdMessage();
					// mBirdMessage.birdId = o.getString("birdId");
					// mBirdMessage.birdImage = ValueStack.IMAGEROOT
					// + o.getString("birdImage");
					// mBirdMessage.birdName = o.getString("birdName");
					// mBirdMessage.collectLoaction = o
					// .getString("collectLoaction");
					// mBirdMessage.date = o.getString("date");
					// mBirdMessage.deadDes = o.getString("deadDes");
					// mBirdMessage.deadImage = ValueStack.IMAGEROOT
					// + o.getString("deadImage");
					// mBirdMessage.deadNum = o.getString("deadNum");
					// mBirdMessage.direct = o.getString("direct");
					// mBirdMessage.distance = o.getString("distance");
					// mBirdMessage.healthNum = o
					// .getString("healthNum");
					// mBirdMessage.kind = o.getString("kind");
					// mBirdMessage.latitude = o.getString("latitude");
					// mBirdMessage.longitude = o
					// .getString("longitude");
					// mBirdMessage.num = o.getString("num");
					// mBirdMessage.place = o.getString("place");
					// mBirdMessage.sickDes = o.getString("sickDes");
					// mBirdMessage.sickImage = ValueStack.IMAGEROOT
					// + o.getString("sickImage");
					// mBirdMessage.sickNum = o.getString("sickNum");
					// mBirdMessage.status = o.getString("status");
					// mBirdMessage.stayPlace = o
					// .getString("stayPlace");
					// params[0].addBirdMessage(mBirdMessage);
					// }
					// mDataService.saveBirdMessage(params[0]);
					// } else {
//					code = 0;
					// }
					// } catch (Exception e) {
					// Log.i("Exception"+e.getMessage());
					// }
//				} else {// 有数据进行查询
					mDataService.getUnuploadBirdList(params[0]);
					code = 1;
					Log.i("有数据进行查询");
//				}
			}
		}
		return code;
	}

	@Override
	protected void onPostExecute(Integer result) {
		if (!iscancle && callback != null) {
			if (result == 1) {
				callback.onComplete(result);
			}
		}

	}

	private boolean iscancle = false;

	public void release() {
		callback = null;
	}
}
